package com.easy.module.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.framework.common.utils.ConvertUtil;
import com.easy.framework.security.model.OauthClientDetails;
import com.easy.framework.security.service.IOauthClientDetailsService;
import com.easy.module.system.entity.SysOauthClientDetails;
import com.easy.module.system.mapper.SysOauthClientDetailsMapper;
import com.easy.module.system.service.ISysOauthClientDetailsService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 终端信息Service业务层处理
 *
 * @author Yang Huijing (Email:1073602@qq.com)
 * @date 2024-04-11
 */
@Service
public class SysOauthClientDetailsServiceImpl extends ServiceImpl<SysOauthClientDetailsMapper, SysOauthClientDetails> implements ISysOauthClientDetailsService, IOauthClientDetailsService {

    @Override
    public SysOauthClientDetails getByClientId(String clientId) {
        if (StringUtils.hasText(clientId)) {
            return getOne(Wrappers.<SysOauthClientDetails>lambdaQuery().eq(SysOauthClientDetails::getClientId, clientId), false);
        }
        return null;
    }

    @Override
    public OauthClientDetails loadClientByClientId(String clientId) {
        return ConvertUtil.convert(getByClientId(clientId), OauthClientDetails.class);
    }

    @Override
    public List<SysOauthClientDetails> getUserCorrelationClientList(Long userId) {
        if (null != userId) {
            return list(Wrappers.<SysOauthClientDetails>lambdaQuery()
                    .eq(SysOauthClientDetails::getCreatedBy, userId)
                    .or()
                    .eq(SysOauthClientDetails::getUpdatedBy, userId));
        }
        return null;
    }
}
